/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye.ui.component.actionbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.devoxy.roboeye.R;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 25.06.11
 * Time: 18:17
 */

// TODO running string in caption if need
public class ActionBar extends LinearLayout {

    private TextView caption;
    private View logo;
    private LinearLayout home;

    private int lastWidth;
    private int lastHeight;

    private int childrenStartCoordinate = -1; // left coordinate of first children element or -1 if children elements doesn't exists

    public ActionBar(Context context) {
        super(context);
        init();
    }

    public ActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.actionbar_attrs);
        String captionString = array.getString(R.styleable.actionbar_attrs_caption);
        updateCaption(captionString);
        array.recycle();
    }

    private void updateCaption(CharSequence captionString) {
        if (!TextUtils.isEmpty(captionString)) {
            logo.setVisibility(GONE);
            home.setVisibility(VISIBLE);
            caption.setText(captionString);
        } else {
            logo.setVisibility(VISIBLE);
            home.setVisibility(GONE);
        }
    }

    private void init() {
        inflate(getContext(), R.layout.actionbar, this);
        caption = (TextView) findViewById(R.id.actionbar_caption);
        logo = findViewById(R.id.actionbar_logo);
        home = (LinearLayout) findViewById(R.id.actionbar_home);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = r - l;
        int height = b - t;
        if (lastWidth == width && lastHeight == height) {
            return;
        }

        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE && !(child instanceof FrameLayout)) {
                int nl = r - ((count - i) * (b - t));
                int nt = t;
                int nr = r - ((count - i) * (b - t)) + (b - t);
                int nb = b;
                child.layout(nl, nt, nr, nb);
                //TODO do not hardcode, and translate values from dp to px
                addDelimiter(nl - 3, nt, nl - 1, nb);

                // update first children coordinate if needed
                if (childrenStartCoordinate == -1 || childrenStartCoordinate > nl - 3) {
                    childrenStartCoordinate = nl - 3;
                    // TODO FIX ME
                    // open in file browse activity catalog with long path
                    // OK
                    // open file browse activity
                    // caption truncated and doesn't contain '...'
                    layoutCaption();
                }
            } else {
                super.onLayout(changed, l, t, r, b);
            }
        }

        lastWidth = width;
        lastHeight = height;
    }

    private void addDelimiter(int l, int t, int r, int b) {
        ImageView delimiter = new ImageView(getContext());
        delimiter.setBackgroundResource(R.drawable.delimiter);
        delimiter.layout(l, t, r, b);
        addView(delimiter);
    }

    public void setCaption(CharSequence text) {
        caption.setText(text);
        updateCaption(text);
    }

    private void layoutCaption() {
        int l = caption.getLeft();
        int t = caption.getTop();
        int b = caption.getBottom();
        int r;
        if (childrenStartCoordinate == -1) {
            r = caption.getRight();
        } else {
            r = childrenStartCoordinate;
        }
        caption.layout(l, t, r, b);
    }

    public CharSequence getCaption() {
        return caption.getText();
    }
}
